home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / share / pygtk / 2.0 / codegen / defsparser.py < prev    next >
Text File  |  2006-01-20  |  4KB  |  134 lines

  1. # -*- Mode: Python; py-indent-offset: 4 -*-
  2. import os, sys
  3. import scmexpr
  4. from definitions import *
  5.  
  6. class IncludeParser(scmexpr.Parser):
  7.     """A simple parser that follows include statements automatically"""
  8.     def include(self, filename):
  9.         if not os.path.isabs(filename):
  10.             filename = os.path.join(os.path.dirname(self.filename), filename)
  11.  
  12.         # set self.filename to the include name, to handle recursive includes
  13.         oldfile = self.filename
  14.         self.filename = filename
  15.         self.startParsing()
  16.         self.filename = oldfile
  17.  
  18. class DefsParser(IncludeParser):
  19.     def __init__(self, arg, defines={}):
  20.     IncludeParser.__init__(self, arg)
  21.     self.objects = []
  22.         self.interfaces = []
  23.     self.enums = []      # enums and flags
  24.         self.boxes = []      # boxed types
  25.         self.pointers = []   # pointer types
  26.     self.functions = []  # functions and methods
  27.     self.virtuals = []   # virtual methods
  28.     self.c_name = {}     # hash of c names of functions
  29.     self.methods = {}    # hash of methods of particular objects
  30.     self.defines = defines        # -Dfoo=bar options, as dictionary
  31.  
  32.     def define_object(self, *args):
  33.     odef = apply(ObjectDef, args)
  34.     self.objects.append(odef)
  35.     self.c_name[odef.c_name] = odef
  36.     def define_interface(self, *args):
  37.         idef = apply(InterfaceDef, args)
  38.         self.interfaces.append(idef)
  39.         self.c_name[idef.c_name] = idef
  40.     def define_enum(self, *args):
  41.         edef = apply(EnumDef, args)
  42.         self.enums.append(edef)
  43.         self.c_name[edef.c_name] = edef
  44.     def define_flags(self, *args):
  45.         fdef = apply(FlagsDef, args)
  46.         self.enums.append(fdef)
  47.         self.c_name[fdef.c_name] = fdef
  48.     def define_boxed(self, *args):
  49.         bdef = apply(BoxedDef, args)
  50.         self.boxes.append(bdef)
  51.         self.c_name[bdef.c_name] = bdef
  52.     def define_pointer(self, *args):
  53.         pdef = apply(PointerDef, args)
  54.         self.pointers.append(pdef)
  55.         self.c_name[pdef.c_name] = pdef
  56.     def define_function(self, *args):
  57.     fdef = apply(FunctionDef, args)
  58.     self.functions.append(fdef)
  59.     self.c_name[fdef.c_name] = fdef
  60.     def define_method(self, *args):
  61.     mdef = apply(MethodDef, args)
  62.     self.functions.append(mdef)
  63.     self.c_name[mdef.c_name] = mdef
  64.     def define_virtual(self, *args):
  65.     vdef = apply(VirtualDef, args)
  66.     self.virtuals.append(vdef)
  67.     def merge(self, old, parmerge):
  68.         for obj in self.objects:
  69.             if old.c_name.has_key(obj.c_name):
  70.                 obj.merge(old.c_name[obj.c_name])
  71.         for f in self.functions:
  72.             if old.c_name.has_key(f.c_name):
  73.                 f.merge(old.c_name[f.c_name], parmerge)
  74.  
  75.     def printMissing(self, old):
  76.     for obj in self.objects:
  77.         if not old.c_name.has_key(obj.c_name):
  78.                 obj.write_defs()
  79.     for f in self.functions:
  80.         if not old.c_name.has_key(f.c_name):
  81.         f.write_defs()
  82.  
  83.     def write_defs(self, fp=sys.stdout):
  84.     for obj in self.objects:
  85.         obj.write_defs(fp)
  86.     for enum in self.enums:
  87.         enum.write_defs(fp)
  88.         for boxed in self.boxes:
  89.             boxed.write_defs(fp)
  90.         for pointer in self.pointers:
  91.             pointer.write_defs(fp)
  92.     for func in self.functions:
  93.         func.write_defs(fp)
  94.  
  95.     def find_object(self, c_name):
  96.         for obj in self.objects:
  97.             if obj.c_name == c_name:
  98.                 return obj
  99.         else:
  100.             raise ValueError, 'object not found'
  101.  
  102.     def find_constructor(self, obj, overrides):
  103.         for func in self.functions:
  104.             if isinstance(func, FunctionDef) and \
  105.                func.is_constructor_of == obj.c_name and \
  106.                not overrides.is_ignored(func.c_name):
  107.                 return func
  108.  
  109.     def find_methods(self, obj):
  110.         objname = obj.c_name
  111.         return filter(lambda func, on=objname: isinstance(func, MethodDef) and
  112.                       func.of_object == on, self.functions)
  113.  
  114.     def find_virtuals(self, obj):
  115.         objname = obj.c_name
  116.         retval = filter(lambda func, on=objname: isinstance(func, VirtualDef) and
  117.                         func.of_object == on, self.virtuals)
  118.         return retval
  119.  
  120.     def find_functions(self):
  121.         return filter(lambda func: isinstance(func, FunctionDef) and
  122.                       not func.is_constructor_of, self.functions)
  123.  
  124.     def ifdef(self, *args):
  125.     if args[0] in self.defines:
  126.         for arg in args[1:]:
  127.         self.handle(arg)
  128.  
  129.     def ifndef(self, *args):
  130.     if args[0] not in self.defines:
  131.         for arg in args[1:]:
  132.         self.handle(arg)
  133.  
  134.